// subPages/center/property/property.js
const app = getApp();
var util =  require('../../../utils/util.js');
var loadMoreView,p = 1,num = app.globalData.pageNum,type = 'property'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    downImg: '/resources/theme/down.png',
    upImg: '/resources/theme/up.png',
    date: '',
    isDate: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    loadMoreView = that.selectComponent("#loadMoreView")
    that.setData({
      dateImgSrc: that.data.downImg,
    })
    that.setDate()
    //社区id
    if(app.globalData.communityId){
      that.setData({
        communityId: app.globalData.communityId
      });
      that.getPropertyData()
    }else{
      app.communityIdCallback = communityId => {
        if (communityId != '') {
          that.setData({
            communityId: communityId
          });
          that.getPropertyData()
        }
      }
    }
  },

  //设置日期
  setDate:function(){
    let dateStart = util.setTime(new Date())
    let Y = dateStart.split('-')
    this.setData({
      dateStart: Y[0]- 3,
      dateEnd: dateStart,
    })
  },

  bindDateChange: function(e) {
    this.setData({
      isDate: true,
      date: e.detail.value,
      dateImgSrc: this.data.downImg
    })
    this.getPropertyData()
  },

  bindDateCancel:function(){
    this.setData({
      dateImgSrc: this.data.downImg
    })
  },

  //选择箭头
  imgChange:function(e){
    var that = this
    if(that.data.dateImgSrc == that.data.downImg){
      that.setData({
        dateImgSrc: that.data.upImg
      })
    }
  },

  //获取物业公告
  getPropertyData:function(){
    util.getAjax(app.globalData.apiUrl +'/get_community_member_center_property_notice',this.getProperty,{type:2,community_id: this.data.communityId,date: this.data.date,p: p,num: num},this.getPropertyErr)
  },

  getProperty(res){
    // console.log(res)
    var that = this 
    if(res.data.list.length != 0){
      var list = []
      if(p == 1) {
        list = res.data.list
      } else {
        list = that.data.propertyList.concat(res.data.list)
      }
      that.setData({
        propertyList: list
      })
    }else{
      that.setData({
        propertyList: []
      })
    }
    loadMoreView.loadMoreComplete(res.data,type)
  },

  //获取物业公告数据异常
  getPropertyErr(err){
    if(p>1){
      loadMoreView.loadMoreFail()
    }else{
      util.showModal('提示','获取物业公告失败，请向上滑动屏幕')
      this.setData({
        isFail: true
      })
    }
  },

  loadMoreListener: function(e) {
    p += 1
    this.getPropertyData()
  },

  clickLoadMore: function(e) {
    this.getPropertyData()
  },


  //删除标签
  delete:function(){
    this.setData({
      isDate: false,
      date: ''
    })
    this.getPropertyData()
  },

  //物业详情
  property:function(e){
    wx.navigateTo({
      url: '../propertyDetails/propertyDetails?id=' + e.currentTarget.dataset.id,
    })
  },

  //回到顶部
  goTop: function (e) {
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },

  // 获取滚动条当前位置
  onPageScroll: function (e) {
    // console.log(e)
    if (e.scrollTop > 600) {
      this.setData({
        isTop: true
      });
    } else {
      this.setData({
        isTop: false
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.isFail){
      this.getPropertyData()
      return
    }
    loadMoreView.loadMore()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})